package com.xinbochuang.machine.admin.controller.macroom;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.xinbochuang.machine.admin.dto.EquipmentDetailDto;
import com.xinbochuang.machine.admin.service.macRoom.IEquipmentDetailService;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;

/**
 *
 * @author 杜文博
 * @date 2020-09-08
 */
@RestController
@RequestMapping("/equipment-detail")
public class EquipmentDetailController {

	@Autowired
	IEquipmentDetailService iEquipmentDetailService;

//	@GetMapping("/list")
//	public AjaxResult getEquDetailList(HttpServletResponse response, @RequestParam(value = "macRoom") String
//		macRoom){
//		AjaxResult ajaxResult = new AjaxResult();
//		List<EquipmentDetail> list = iEquipmentDetailService.findEquDetailByMacRoom(macRoom);
//		if(list.size()>=0){
//			ajaxResult = AjaxResult.ok();
//			ajaxResult.put("result",list);
//		}else {
//			ajaxResult = AjaxResult.error();
//		}
//		return ajaxResult;
//	}

	/**
	 * 获取资产列表
	 * @param cabinetId
	 * @return
	 */
	@GetMapping("/list")
	public AjaxResult list(@RequestParam(value = "cabinetId") String cabinetId,@RequestParam(value = "pageNum")
		String pageNum,@RequestParam(value = "pageSize") String pageSize ) {
			List<EquipmentDetailDto> list = iEquipmentDetailService.findEquDetailByCabinetId(cabinetId,pageNum,pageSize);
			Integer totalCount =iEquipmentDetailService.equDetailTotalCount(cabinetId,pageNum,pageSize);
			return AjaxResult.data(list,totalCount) ;
	}

	/**
	 * 资产列表导入
	 * @param file
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/importEqupmentDetail")
	public AjaxResult importEqupmentDetail(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws
		IOException {
		// 项目在容器中实际发布运行的根路径
		String realPath =request .getSession().getServletContext().getRealPath("/");
		// 自定义的文件名称
		String trueFileName = String.valueOf(System.currentTimeMillis()) + new Random().nextInt();
		String path = realPath + trueFileName;
		File file1 = new File(path);
		file.transferTo(file1);

		ExcelReader reader = ExcelUtil.getReader(file1);
		List<Map<String, Object>> maps = reader.readAll();
		Integer result = iEquipmentDetailService.importEquipmentDetail(maps);
		AjaxResult ajaxResult = new AjaxResult();
		if(result!=0){
			ajaxResult = AjaxResult.ok();
		}else{
			ajaxResult = AjaxResult.error();
		}
		return ajaxResult;
	}

}

